// pages/debtorBaseInfo/odi/odi.ts
import { ZCC_TYPE } from "../../../../../typings/types/common";
import { Debtor } from "../../../../../typings/types/debtor";
import { econKindAPI, investAPI } from "../../../../api/debtor";
import { ZCC } from "../../../../utils/BasePage";
import { formateDigit, validStr } from "../../../../utils/util";

interface PageData {
  econKind: string
  status: string
  stockPercent: number
  companyId: string
  filter_stockPercent: Debtor.FilterModel
  filter_econKind: Debtor.FilterModel
  filter_status: Debtor.FilterModel
  // 
  searchInputClass: string
  placeholderText: string
  keyWords: string
  searchDisabled: boolean
}

new ZCC.RefreshListPage<Debtor.IDOCell>({
  // 点击搜索
  searchFocus() {
    this.setData({
      searchInputClass: "input-is-focus",
      placeholderText: "请输入申请通过的企业名称",
    }, () => {
      this.setData({
        // keyWords: "",
        searchDisabled: false
      })
    })
  },
  // 收起
  searchBlur() {
    this.setData({
      searchInputClass: "",
      placeholderText: "搜索...",
      // keyWords: this.data.name,
      searchDisabled: true
    })
  },
  // 搜索框输入关键字
  searchChange(e: any) {
    console.log(this.data.keyWords)
    this.onPullDownRefresh()
  },
  onChange(e: any) {
    console.log(e)
    const id = e.currentTarget.id
    const value = e.detail.value
    switch (id) {
      case "1":
        this.setData({
          stockPercent: Number(value) || 0,
        })
        break
      case "2":
        this.setData({
          econKind: value,
        })
        break
      case "3":
        this.setData({
          status: value,
        })
        break
      default: break
    }

    this.onPullDownRefresh()
  },
  getOdiInfo: async function () {

    this.setRefreshing()
    // wx.showLoading()
    try {
      console.log(this.data.page)
      const res = await investAPI(
        this.data.companyId,
        this.data.page,
        this.data.econKind === '全部' ? '' : this.data.econKind,
        this.data.keyWords,
        this.data.status === '全部' ? '' : this.data.status,
        this.data.stockPercent)//;Number(this.data.filter_stockPercent.value)
      if (res.content) {
        const list = res.content.list
        console.log(res.content)
        let items: Debtor.IDOCell[] = []
        if (list.length > 0) {
          items = list.map((item) => {
            return {
              name: validStr(item.name),
              status: validStr(item.status),
              type: validStr(item.econKind),
              rows: [
                { title: '出资比例', val: validStr((item.stockPercent || 0).toFixed(2)) + '%', sectitle: '认缴出资时间', secval: item.shouldDate },
                { title: '认缴出资额', val: formateDigit(item.shouldCapi), sectitle: '', secval: '', valsubfix: '万元' },
              ]
            }
          })

          //   items = this.data.items.concat(items)
          //   const page = ++this.data.page
          //   this.setData({
          //     page: page,
          //     items: items,
          //     total: res.content.total || 0
          //   })
          // } else {
          //   this.setData({
          //     total: res.content.total || 0
          //   })
        }
        this.updateDatasWithNewItems(items, res.content.total, res.content.hasNextPage)

      }
    } finally {
      this.endRefreshing()
      // wx.stopPullDownRefresh()
      // wx.hideLoading()
    }
  },

  getEncoKind: async function () {
    try {
      const res = await econKindAPI(this.data.companyId)
      if (res.content) {
        const content = res.content
        console.log(res.content)
        let items: ZCC_TYPE.Option[] = [{ label: '全部', value: '' }]
        items = items.concat(content.map((item) => {
          return {
            label: item,
            value: item
          }
        }))

        this.data.filter_econKind.options = items
        this.setData({
          filter_econKind: this.data.filter_econKind
        })
      }
    } finally {
      // wx.stopPullDownRefresh()
      // wx.hideLoading()
    }
  },
  /**
   * 页面的初始数据
   */
  data: <PageData>{
    stockPercent: 0,
    econKind: '',
    status: '',
    companyId: '',
    searchInputClass: '',
    placeholderText: '搜索...',
    keyWords: '',
    searchDisabled: true,
    filter_stockPercent: {
      value: '',
      options: [
        { label: '全部', value: '' },
        { label: '100%', value: '1' },
        { label: '66.66%以上', value: '2' },
        { label: '50%以上', value: '3' },
        { label: '33.33%以上', value: '4' },
        { label: '25%以上', value: '5' },
        { label: '5%以上', value: '6' }
      ]
    },
    filter_econKind: {
      value: '',
      options: [
        { label: '全部', value: '' }]
    },
    filter_status: {
      value: '',
      options: [
        { label: '全部', value: '' },
        { label: '吊销', value: '吊销' },
        { label: '迁出', value: '迁出' },
        { label: '注销', value: '注销' },
        { label: '停业', value: '停业' },
        { label: '撤销', value: '撤销' },
        { label: '存续(在营、开业、在册)', value: '存续(在营、开业、在册)' }
      ]
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    console.log('onLoad = ' + JSON.stringify(this.options))
    this.setData({
      companyId: this.options.companyId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getEncoKind()
    this.getOdiInfo()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // this.setData({
    //   page: 1,
    //   items: [],
    //   total: undefined
    // })
    if (this.resetPageIfNeed()) {
      this.getOdiInfo()
    }

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.hasNextPage) {
      this.getOdiInfo()
    }

  },

})